package com.example.test.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.test.AddCarNumberActivity;
import com.example.test.R;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.DbUtils.DaoConfig;
import com.lidroid.xutils.view.annotation.ViewInject;

public class CarNumberFragment extends BaseFragment {

	@ViewInject(R.id.lv_carnumberlist)
	private ListView lv_carnumberlist;
	@ViewInject(R.id.btn_addcarnumber)
	private Button btn_addcarnumber;
	private List<String> list = new ArrayList<String>();
	private DbDaoUtils dao;
	private CarNumberListAdapter adapter;

	@Override
	public void initData() {
		
		// TODO Auto-generated method stub
		checkCarNumber();
		btn_addcarnumber.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(ct, AddCarNumberActivity.class);
				startActivity(intent);
			}
		});
	}

	private int[] ids2 = { R.color.gridcolor1, R.color.gridcolor2,
			R.color.gridcolor3, R.color.gridcolor4, R.color.gridcolor5,
			R.color.gridcolor6 };
	
	private void checkCarNumber() {
		if(dao==null){
		dao = new DbDaoUtils(ct);
		}
		list = dao.findAllCarNumber();
		if(adapter==null){
		adapter = new CarNumberListAdapter();
        lv_carnumberlist.setAdapter(adapter);
		}
		else{
			adapter.notifyDataSetChanged();
		}
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		checkCarNumber();
	}

	@Override
	public View initView(LayoutInflater inflater) {

		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.carnumber_layout, null);
		ViewUtils.inject(this, view);
		return view;
	}

	class CarNumberListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = View.inflate(ct, R.layout.carnumberitem_layout,
						null);
				holder = new ViewHolder();
				
				holder.tv_carnumber = (TextView) convertView
						.findViewById(R.id.tv_carnumber);
				convertView.setTag(holder);
			}
            
			holder.tv_carnumber.setText(list.get(position));
			int i=position%ids2.length;
		    if(i>=6){
		    	i=6;
		    }
			holder.tv_carnumber.setBackgroundResource(ids2[i]);
			return convertView;
		}

	}

	public static class ViewHolder {
		TextView tv_carnumber;
		LinearLayout ly_carnumber;

	}
}
